// pages/search/search.js
Page({


  data: {
    findFileList:[],
    hasNoRes:false,
    source:"",
    searchPostList:[],
  },
  onLoad: function (options) {
    let source = options.source
    this.setData({
      search: this.search.bind(this),
      source:source,
    })
  },
  clear:function () {
    this.setData({hasNoRes:false})
  },
  search: function (value) {
    let app = getApp()
    if(value.replace(/(^\s*)|(\s*$)/g, "") != ""){
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          if(this.data.source == "library"){
            wx.request({
              url: app.globalData.url + '/file/findfile',
              data:{
                key:value,
              },
              success: result => {
                let findFileList = result.data.data
                if(findFileList.length == 0){
                  this.setData({hasNoRes:true})
                }else{
                  this.setData({hasNoRes:false})
                  for(let i = 0; i < findFileList.length; ++i){
                    findFileList[i]["text"] = findFileList[i].fileName
                  }
                  this.setData({
                    findFileList:findFileList,
                  })
                  app.globalData.searchFileList = findFileList
                  resolve(findFileList)
                }
              }
            })
          }else if(this.data.source == "community"){
            wx.request({
              url: app.globalData.url + '/community/search',
              data:{
                key:value,
              },
              success: result => {
                let searchPostList = result.data.data
                if(searchPostList.length == 0){
                  this.setData({hasNoRes:true})
                }else{
                  this.setData({hasNoRes:false})
                  for(let i = 0; i < searchPostList.length; ++i){
                    searchPostList[i]["text"] = searchPostList[i].subtitle
                  }
                  this.setData({
                    searchPostList:searchPostList,
                  })
                  app.globalData.searchPostList = searchPostList
                  resolve(searchPostList)
                }
              }
            })
          }
        }, 200)
      })
    }
  },

  selectResult: function (e) {
    console.log('select result', e.detail)
    let app = getApp()
    if(this.data.source == "library"){
      app.globalData.selectedSearchFile = e.detail
      wx.navigateTo({
        url: '/pages/show_file_search/show_file_search',
      })
    }else if(this.data.source == "community"){
      app.globalData.selectedSearchPost = e.detail
      wx.navigateTo({
        url: '/pages/detail/detail?source=search',
      })
    }
    
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})